/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Date;
import java.util.Properties;
/**
 *
 * @author Kenny
 */
public class GuiMail {
    //public static final String USERNAME="smurflteddy";
    //public static final String PASSWORD="sieunhan";
    public String USERNAME="";
    public String PASSWORD="";

/*
   public static void sendMail(String mailFrom, String mailTo, String mailSubject, String mailText, String filename) throws Exception {
        //
        // Email information such as from, to, subject and contents.
        //
      String  mailFrom = "linhnq8x@gmail.com";
     String   mailTo = "smurflteddy@gmail.com";
     String   mailSubject = "SSL - Gmail Send Email Demo";
     String   mailText = "SSL - Gmail Send Email Demo";
      String  filename = "C:/Users/Kenny/Desktop/send.jpg";

        SendMail gmail = new SendMail();
        gmail.sendMail(mailFrom, mailTo, mailSubject, mailText, filename);
    }
*/
    public  void sendMail(String mailFrom, String mailTo,
            String mailSubject, String mailText, String filename, final String USERNAME, final String PASSWORD)
            // final String USERNAME: "FINAL " Truyen vao ham getPasswordAuthentication ben duoi de xac thuc tai khoan email
            throws Exception {

        Properties config = createConfiguration();

        //
        // Creates a mail session. We need to supply username and
        // password for Gmail authentication.
        //
        Session session = Session.getInstance(config, new Authenticator() {
            @Override
    
    
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(
                        USERNAME,
                        PASSWORD);
            }
        });

        //
        // Creates email message
        //
        Message message = new MimeMessage(session);
        message.setSentDate(new Date());
        message.setFrom(new InternetAddress(mailFrom));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
        message.setSubject(mailSubject);
        message.setText(mailText);

        //
        // Set the email message text.
        //
        MimeBodyPart messagePart = new MimeBodyPart();
        messagePart.setText(mailText);

        //
        // Set the email attachment file
        //
        MimeBodyPart attachmentPart = new MimeBodyPart();
        FileDataSource fileDataSource = new FileDataSource(filename) {
            @Override
            public String getContentType() {
                return "application/octet-stream";
            }
        };
        attachmentPart.setDataHandler(new DataHandler(fileDataSource));
        attachmentPart.setFileName(fileDataSource.getName());

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messagePart);
        multipart.addBodyPart(attachmentPart);

        message.setContent(multipart);



        //
        // Send a message
        //
        Transport.send(message);
    }

    private Properties createConfiguration() {
        return new Properties() {
            {
                put("mail.smtp.host", "smtp.gmail.com");
                put("mail.smtp.auth", "true");
                put("mail.smtp.port", "465");
                put("mail.smtp.socketFactory.port", "465");
                put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            }
        };
    }
}
